﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS发运计划创建
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSShipping_PlanCreateController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public TMSShipping_PlanCreateController(ILogger<TMSShipping_PlanCreateRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            _logger = loggerFactory.CreateLogger<TMSShipping_PlanCreateController>();

        }

        /// <summary>
        /// TMS发运计划创建
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostShipping_PlanCreate([FromBody] TMSShipping_PlanCreateRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSShipping_PlanCreateResponseDto re = new TMSShipping_PlanCreateResponseDto();
            string factory_code = strparm.factory_code;
            string factory_id = "";


            if (factory_code=="C01")
            {
                factory_id = "[\"tms_zh\"]";
            } 
            else if (factory_code == "E01")
            {
                factory_id = "[\"tms_sc\"]";
            }
            else if(factory_code == "B01")
            {
                factory_id = "[\"tms_qh\"]";
            }
            else if(factory_code == "A01")
            {
                factory_id = "[\"tms_gd\"]";
            }
            else if (factory_code == "G01")
            {
                factory_id = "[\"tms_ZN\"]";
            }
            else if (factory_code == "H01")
            {
                factory_id = "[\"tms_gz\"]";
            }
            else
            {
                strMsg = "factory_code=<"+ factory_code + "> not fine ";
            }

            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            Dictionary<string, string> plandic = new Dictionary<string, string>(); //计划表插入dic
            try
            {
                if (strMsg == "")
                {
                    var row = strparm.rows;
                    foreach (var item in row)
                    {

                        plandic = new Dictionary<string, string>();
                        dic = new Dictionary<string, string>();
                        dic.Add("ccode", item.ccode);
                        DataTable check_ccode = MethodHelp.GetTableInfo("TMS0003", dic, con);//查询当前计划是否存在
                        if (check_ccode.Rows.Count > 0)
                        {
                            strMsg = strMsg + "Flowing ID<" + item.ccode + "> is Repeat";
                        }
                        else
                        {
                            plandic.Add("factory_code", factory_id);
                            plandic.Add("ccode", item.ccode);
                            plandic.Add("ddate", item.ddate);
                            plandic.Add("customer_name", item.customer_name);
                            plandic.Add("s_address_value", item.s_address_value);
                            plandic.Add("t_address_value", item.t_address_value);
                            plandic.Add("customerpo", item.customerpo);
                            plandic.Add("salesman", item.salesman);
                            plandic.Add("sotype", item.sotype);
                            plandic.Add("salesorderno", item.salesorderno);
                            plandic.Add("materialcode", item.materialcode);
                            plandic.Add("materialname", item.materialname);
                            plandic.Add("specifications", item.specifications);
                            plandic.Add("remarks", item.remarks);
                            plandic.Add("resistance", item.resistance);
                            plandic.Add("materialamount", item.materialamount.ToString());
                            plandic.Add("materialweight", item.materialweight.ToString());
                            plandic.Add("carnum", item.carnum);
                            plandic.Add("factory_code_value", factory_code);
                            plandic.Add("salesorderline", item.salesorderline);
                            plandic.Add("producttype", item.producttype);
                            plandic.Add("stickmodel", item.stickmodel);
                            plandic.Add("sticksize", item.sticksize);
                            plandic.Add("mthickness", item.mthickness);
                            plandic.Add("mgrade", item.mgrade);
                            plandic.Add("mchamfer", item.mchamfer);
                            plandic.Add("marginslength", item.marginslength);
                            plandic.Add("marginswide", item.marginswide);
                            plandic.Add("marginsdiagonal", item.marginsdiagonal);
                            plandic.Add("deliverno", item.deliverno);
                            plandic.Add("contacts_user", item.contacts_user);
                            plandic.Add("contacts_phone_num", item.contacts_phone_num);
                            plandic.Add("inner_salesorderno", item.inner_salesorderno);
                            plandic.Add("delivery_type", item.delivery_type);
                            plandic.Add("out_remark", item.out_remark);
                            plandic.Add("is_inner_order", item.is_inner_order);
                            plandic.Add("customer_batch_no", item.customer_batch_no);
                            string strSQL = MethodHelp.GetTranSQL("TMS0002", plandic, con);
                            if (strSQL != "")
                            {
                                strSqlList.Add(strSQL.ToString());
                            }
                            else
                            {
                                strMsg = "Parameter conversion failed";
                            }

                        }
                    }
                    if (strMsg == "")
                    {
                        if (strSqlList.Count > 0)
                        {
                            bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                            if (A)
                            {

                            }
                            else
                            {
                                strMsg = "Database update failed";
                            }

                        }
                        else
                        {
                            strMsg = "No insertion data present";
                        }
                    }
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
